﻿using System;
using System.Collections.Generic;
using System.Text;
using DBScript;
using eBlog.Cache;
using eBlog.Entity;

namespace eBlog.Service
{
  public  class PostsService
    {

       
      #region Posts services
        /// <summary>
        /// get posts'count
        /// </summary>
        /// <returns> count</returns>
        public static  int GetPostsCount()
        {
            if(CacheService<int>.IsCacheExist("PostCount"))
            {
                return CacheService<int>.GetCacheItem("PostCount");
            }
            else
            {
                IDBBase dal = DBFactory.GetDbProvider();

                int count=dal.From("Posts", "id").GetCount();

                CacheService<int>.AddCache(count, "PostCount");//add postCount to cache
                return count;
            }
            
        }

        /// <summary>
        /// get posts'count by typeid
        /// </summary>
        /// <param name="typeId">typeid</param>
        /// <returns>count</returns>
        public static int GetPostsCount(int typeId)
        {
            if (CacheService<int>.IsCacheExist("TypePostCount"))
            {
                return CacheService<int>.GetCacheItem("TypePostCount");
            }
            else
            {
                IDBBase dal = DBFactory.GetDbProvider();

                int count=dal.From("Posts", "id").Where("TypeID='" + typeId + "'").GetCount();

                CacheService<int>.AddCache(count, "TypePostCount");
                return count;
            }

            
        }

      /// <summary>
      /// 
      /// </summary>
      /// <param name="tagsId"></param>
      /// <returns></returns>
        public static int GetPostsCountByTags(int tagsId)
        {
            if (CacheService<int>.IsCacheExist("TagsPostCount"))
            {
                return CacheService<int>.GetCacheItem("TagsPostCount");
            }
            else
            {
                IDBBase dal = DBFactory.GetDbProvider();
                int count = dal.From("Posts", "id")
                    .Where("Tag like '%," + tagsId + ",%' or Tag like '%," + tagsId + "'or Tag like'" + tagsId +
                           ",%' or Tag ='" + tagsId + "'")
                    .GetCount();

                CacheService<int>.AddCache(count, "TagsPostCount");
                return count;
            }

           
        }

        /// <summary>
        /// get all posts list
        /// </summary>
        /// <param name="page">page</param>
        /// <param name="pageCount">page'count</param>
        /// <returns>page'list</returns>
        public static List<Posts> GetPostsList(int page, int pageCount)
        {

            if(CacheService<List<Posts>>.IsCacheExist("PostList"+page))
            {
                return CacheService<List<Posts>>.GetCacheItem("PostList"+page);
            }
            else
            {
                IDBBase dal = DBFactory.GetDbProvider();
                List<Posts> lists = dal.From("Posts", "id").Where("TopStatus = 0").Page(page, pageCount).GetList<Posts>();

                CacheService<List<Posts>>.AddCache(lists, "PostList"+page);

                foreach (var postsItem in lists)
                {
                    if(!CacheService<Posts>.IsCacheExist(postsItem.ID.ToString()))
                    {
                        CacheService<Posts>.AddCache(postsItem, postsItem.ID.ToString());

                    }

                }
                
                return lists;
            }

         
        }

        public static List<Posts> GetTopPostList(int page,int pageCount)
        {
            if (CacheService<List<Posts>>.IsCacheExist("TopPostList" + page))
            {
                return CacheService<List<Posts>>.GetCacheItem("TopPostList" + page);
            }
            else
            {
                IDBBase dal = DBFactory.GetDbProvider();
                List<Posts> lists = dal.From("Posts", "id").Where("TopStatus=1").Page(page, pageCount).GetList<Posts>();

                CacheService<List<Posts>>.AddCache(lists, "TopPostList" + page);

                foreach (var postsItem in lists)
                {
                    if (!CacheService<Posts>.IsCacheExist(postsItem.ID.ToString()))
                    {
                        CacheService<Posts>.AddCache(postsItem, postsItem.ID.ToString());

                    }

                }

                return lists;
            }
        }

      /// <summary>
        /// get posts list by type
        /// </summary>
        /// <param name="page">page index</param>
        /// <param name="pageCount">page count</param>
        /// <param name="typeId">type ID</param>
        /// <returns>posts list</returns>
        public static List<Posts> GetPostsList(int page, int pageCount, int typeId)
        {
            IDBBase dal = DBFactory.GetDbProvider();
            List<Posts> lists = dal.From("Posts", "id").Page(page, pageCount).Where("TypeID='" + typeId + "'").GetList<Posts>();
            return lists;
        }

        /// <summary>
        /// get posts list by tags
        /// </summary>
        /// <param name="page">page index</param>
        /// <param name="pageCount">page count</param>
        /// <param name="tagsId">tags ID</param>
        /// <returns>posts list</returns>
        public static List<Posts> GetPostsListByTags(int page, int pageCount, int tagsId)
        {


            IDBBase dal = DBFactory.GetDbProvider();
            List<Posts> lists = dal.From("Posts", "id")
                .Page(page, pageCount)
                .Where("Tag like '%," + tagsId + ",%' or Tag like '%," + tagsId + "'or Tag like'" + tagsId + ",%' or Tag ='" + tagsId + "'")
                .GetList<Posts>();
            return lists;
        }

        /// <summary>
        /// get a posts by id
        /// </summary>
        /// <param name="id">posts'id</param>
        /// <returns>post model</returns>
        public static Posts GetPostsById(int id)
        {
            if(CacheService<Posts>.IsCacheExist(id.ToString()))
            {
              return  CacheService<Posts>.GetCacheItem(id.ToString());
            }
            else
            {
                IDBBase dal = DBFactory.GetDbProvider();
                Posts posts = dal.GetModel<Posts>(id);

                CacheService<Posts>.AddCache(posts, posts.ID.ToString());


                return posts;
                
            }
           
        }

        /// <summary>
        /// get post by codename
        /// 
        /// this is using for a single page 
        /// </summary>
        /// <param name="codename">codename</param>
        /// <returns>posts'model</returns>
        public static Posts GetPostsByCodeName(string codename)
        {
            IDBBase dal = DBFactory.GetDbProvider();
            Posts posts = dal.Where(" CodeName=@cname").SetString("cname", codename).GetModel<Posts>();
            return posts;
        }

        /// <summary>
        /// get Recommend news
        /// </summary>
        /// <param name="page">page index</param>
        /// <param name="pageCount">count</param>
        /// <param name="isRecommend">0--UnRecommend 1---Recommend</param>
        /// <returns></returns>
        public static List<Posts> GetRecommendPosts(int page, int pageCount, int isRecommend)
        {
            IDBBase dal = DBFactory.GetDbProvider();
            List<Posts> newlist = dal.From("Posts", "id").Page(page, pageCount).Where("IsRecommend='" + isRecommend + "'").GetList<Posts>();
            return newlist;
        }

        /// <summary>
        /// get posts'list by keyword
        /// </summary>
        /// <param name="keyWords">keyword</param>
        /// <returns>posts'list</returns>
        public static List<Posts> FindPostsByKeyWords(string keyWords)
        {
            IDBBase dal = DBFactory.GetDbProvider();
            List<Posts> postslist = dal.From("Posts", "id").Where("Title like  @tt").SetString("tt", "%" + keyWords + "%").GetList<Posts>();
            return postslist;
        }

        /// <summary>
        /// get posts'list by author
        /// </summary>
        /// <param name="author">keyword of the author</param>
        /// <returns>posts'list</returns>
        public static List<Posts> FindPostsByAuthor(string author)
        {
            IDBBase dal = DBFactory.GetDbProvider();
            List<Posts> newlist = dal.From("Posts", "id").Where("Author like @aut").SetString("aut", "%" + author + "%").GetList<Posts>();
            return newlist;
        }

      public static bool UpdatePostViewCount(int postId,int count)
      {
          IDBBase dal = DBFactory.GetDbProvider();
          var postModel= dal.GetModel<Posts>(postId);
          if(postModel!=null)
          {
              postModel.ViewCount = postModel.ViewCount + count;
              return  dal.Update(postModel);
          }
          else
          {
              return false;
          }
          
      }

      #endregion

      
    }
}
